// comment.controller.ts
import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  Query,
  Req,
} from '@nestjs/common';
import { CommentService } from './comment.service';

import {
  ApiBearerAuth,
  ApiBody,
  ApiOkResponse,
  ApiOperation,
  ApiTags,
} from '@nestjs/swagger';
import { CommentSinger } from '../../entities/comment.entity';
import { PaginatedResponseDto } from '../../common/class/res.class';
import { PageSearchUserDto } from '../../dto/user/user-register.dto';
import { User } from '../../entities/user.entity';
import { CommentSingerDto, PagerDto } from '../../dto/singer/singer.dto';

@ApiTags('评论') // 添加API标签
@ApiBearerAuth()
@Controller('comments')
export class CommentController {
  constructor(private readonly commentService: CommentService) {}
  @ApiOperation({ summary: '分页条件查询评论信息' })
  @ApiOkResponse({ type: PagerDto, description: '分页查询结果' })
  @Get('page')
  async page(
    @Query() dto: PagerDto,
  ): Promise<PaginatedResponseDto<Partial<CommentSinger>>> {
    const [records, total] = await this.commentService.page(dto);
    return {
      msg: '查询成功',
      data: {
        records,
        pageSize: dto.pageSize,
        pageNo: dto.pageNo,
        total,
      },
    };
  }

  @ApiOperation({ summary: '查询所有评论' })
  @ApiOkResponse({ type: CommentSingerDto, description: '查询结果' })
  @Get()
  async findAll(): Promise<CommentSinger[]> {
    return await this.commentService.findAll();
  }

  @ApiOperation({ summary: '根据id或者歌手id查询所有评论' })
  @ApiOkResponse({ type: CommentSinger, description: '查询结果' })
  @ApiBody({
    type: CommentSingerDto,
    description: '根据id或者歌手id查询所有评论',
  })
  @Post('ids')
  async findById(
    @Body() dto: Partial<CommentSingerDto>,
  ): Promise<CommentSinger[]> {
    const res = await this.commentService.findById(dto);

    return res;
  }

  @ApiOperation({ summary: '创建评论' })
  @ApiOkResponse({ type: CommentSinger, description: '查询结果' })
  @ApiBody({
    type: CommentSingerDto,
    description: '创建评论',
  }) // 指定请求体的类型和描述信息
  @Post('save')
  async create(
    @Body() commentData: Partial<CommentSinger>,
    @Req() req,
  ): Promise<any> {
    commentData.userId = req.user.userId;
    const data = await this.commentService.create(commentData);
    return {
      msg: '评论成功',
      data: null,
      code: 200,
    };
  }

  @ApiOperation({ summary: '更新评论' })
  @ApiOkResponse({ type: CommentSinger, description: '查询结果' })
  @ApiBody({ type: CommentSingerDto, description: '更新评论' })
  @Post('edit')
  async update(
    @Body() newData: Partial<CommentSingerDto>,
  ): Promise<CommentSinger> {
    return await this.commentService.update(newData);
  }

  @ApiOperation({ summary: '删除评论' })
  @ApiOkResponse({ type: CommentSinger, description: '查询结果' })
  @Delete('delete')
  async delete(@Body() obj: any): Promise<any> {
    const data = await this.commentService.delete(obj.id);
    return {
      msg: '删除成功',
      data: null,
      code: 200,
    };
  }

  @ApiOperation({ summary: '审批评论 0是1否' })
  @ApiBody({ type: CommentSingerDto, description: '审批评论' })
  @ApiOkResponse({ type: CommentSinger, description: '查询结果' })
  @Put('approve')
  async approve(
    @Body() dto: Partial<CommentSingerDto>,
  ): Promise<CommentSinger> {
    return await this.commentService.approve(dto);
  }
}
